home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 285_01 / bisoninf.2 < prev    next >
Text File  |  1990-07-08  |  52KB  |  1,398 lines

  1. Info file bison.info, produced by Makeinfo, -*- Text -*- from input
  2. file bison.texinfo.
  3.  
  4. This file documents the Bison parser generator.
  5.  
  6. Copyright (C) 1988 Free Software Foundation, Inc.
  7.  
  8. Permission is granted to make and distribute verbatim copies of this
  9. manual provided the copyright notice and this permission notice are
  10. preserved on all copies.
  11.  
  12. Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided also
  14. that the sections entitled ``Bison General Public License'' and
  15. ``Conditions for Using Bison'' are included exactly as in the
  16. original, and provided that the entire resulting derived work is
  17. distributed under the terms of a permission notice identical to this
  18. one.
  19.  
  20. Permission is granted to copy and distribute translations of this
  21. manual into another language, under the above conditions for modified
  22. versions, except that the text of the translations of the sections
  23. entitled ``Bison General Public License'' and ``Conditions for Using
  24. Bison'' must be approved for accuracy by the Foundation.
  25.  
  26.  
  27.  
  28. File: bison.info,  Node: Mfcalc Decl,  Next: Mfcalc Rules,  Prev: Multi-function Calc,  Up: Multi-function Calc
  29.  
  30. Declarations for `mfcalc'
  31. -------------------------
  32.  
  33. Here are the C and Bison declarations for the multi-function
  34. calculator.
  35.  
  36.      %{
  37.      #include <math.h>  /* For math functions, cos(), sin(), etc      */
  38.      #include "calc.h"  /* Contains definition of `symrec' */
  39.      %}
  40.      %union {
  41.      double     val;  /* For returning numbers.              */
  42.      symrec  *tptr;   /* For returning symbol-table pointers */
  43.      }
  44.      
  45.      %token <val>  NUM        /* Simple double precision number  */
  46.      %token <tptr> VAR FNCT   /* Variable and Function           */
  47.      %type  <val>  exp
  48.      
  49.      %right '='
  50.      %left '-' '+'
  51.      %left '*' '/'
  52.      %left NEG     /* Negation--unary minus */
  53.      %right '^'    /* Exponentiation        */
  54.      
  55.      /* Grammar follows */
  56.      
  57.      %%
  58.  
  59. The above grammar introduces only two new features of the Bison
  60. language.  These features allow semantic values to have various data
  61. types (*note Multiple Types::.).
  62.  
  63. The `%union' declaration specifies the entire list of possible types;
  64. this is instead of defining `YYSTYPE'.  The allowable types are now
  65. double-floats (for `exp' and `NUM') and pointers to entries in the
  66. symbol table.  *Note Union Decl::.
  67.  
  68. Since values can now have various types, it is necessary to associate
  69. a type with each grammar symbol whose semantic value is used.  These
  70. symbols are `NUM', `VAR', `FNCT', and `exp'.  Their declarations are
  71. augmented with information about their data type (placed between
  72. angle brackets).
  73.  
  74. The Bison construct `%type' is used for declaring nonterminal
  75. symbols, just as `%token' is used for declaring token types.  We have
  76. not used `%type' before because nonterminal symbols are normally
  77. declared implicitly by the rules that define them.  But `exp' must be
  78. declared explicitly so we can specify its value type.  *Note Type
  79. Decl::.
  80.  
  81.  
  82.  
  83. File: bison.info,  Node: Mfcalc Rules,  Next: Mfcalc Symtab,  Prev: Mfcalc Decl,  Up: Multi-function Calc
  84.  
  85. Grammar Rules for `mfcalc'
  86. --------------------------
  87.  
  88. Here are the grammar rules for the multi-function calculator.  Most
  89. of them are copied directly from `calc'; three rules, those which
  90. mention `VAR' or `FNCT', are new.
  91.  
  92.      input:   /* empty */
  93.              | input line
  94.      ;
  95.      
  96.      line:
  97.                '\n'
  98.              | exp '\n'   { printf ("\t%.10g\n", $1); }
  99.              | error '\n' { yyerrok;                  }
  100.      ;
  101.      
  102.      exp:      NUM                { $$ = $1;                         }
  103.              | VAR                { $$ = $1->value.var;              }
  104.              | VAR '=' exp        { $$ = $3; $1->value.var = $3;     }
  105.              | FNCT '(' exp ')'   { $$ = (*($1->value.fnctptr))($3); }
  106.              | exp '+' exp        { $$ = $1 + $3;                    }
  107.              | exp '-' exp        { $$ = $1 - $3;                    }
  108.              | exp '*' exp        { $$ = $1 * $3;                    }
  109.              | exp '/' exp        { $$ = $1 / $3;                    }
  110.              | '-' exp  %prec NEG { $$ = -$2;                        }
  111.              | exp '^' exp        { $$ = pow ($1, $3);               }
  112.              | '(' exp ')'        { $$ = $2;                         }
  113.      ;
  114.      /* End of grammar */
  115.      %%
  116.  
  117.  
  118.  
  119. File: bison.info,  Node: Mfcalc Symtab,  Prev: Mfcalc Rules,  Up: Multi-function Calc
  120.  
  121. Managing the Symbol Table for `mfcalc'
  122. --------------------------------------
  123.  
  124. The multi-function calculator requires a symbol table to keep track
  125. of the names and meanings of variables and functions.  This doesn't
  126. affect the grammar rules (except for the actions) or the Bison
  127. declarations, but it requires some additional C functions for support.
  128.  
  129. The symbol table itself consists of a linked list of records.  Its
  130. definition, which is kept in the header `calc.h', is as follows.  It
  131. provides for either functions or variables to be placed in the table.
  132.  
  133.      /* Data type for links in the chain of symbols.  */
  134.      struct symrec
  135.      {
  136.        char *name;  /* name of symbol              */
  137.        int type;    /* type of symbol: either VAR or FNCT */
  138.        union {
  139.          double var;           /* value of a VAR  */
  140.          double (*fnctptr)();  /* value of a FNCT */
  141.        } value;
  142.        struct symrec *next;    /* link field    */
  143.      };
  144.      
  145.      typedef struct symrec symrec;
  146.      
  147.      /* The symbol table: a chain of `struct symrec'.  */
  148.      extern symrec *sym_table;
  149.      
  150.      symrec *putsym ();
  151.      symrec *getsym ();
  152.  
  153. The new version of `main' includes a call to `init_table', a function
  154. that initializes the symbol table.  Here it is, and `init_table' as
  155. well:
  156.  
  157.      #include <stdio.h>
  158.      
  159.      main()
  160.      {
  161.        init_table ();
  162.        yyparse ();
  163.      }
  164.      
  165.      yyerror (s)  /* Called by yyparse on error */
  166.           char *s;
  167.      {
  168.        printf ("%s\n", s);
  169.      }
  170.      
  171.      struct init
  172.      {
  173.        char *fname;
  174.        double (*fnct)();
  175.      };
  176.      
  177.      struct init arith_fncts[]
  178.        = {
  179.            "sin", sin,
  180.            "cos", cos,
  181.            "atan", atan,
  182.            "ln", log,
  183.            "exp", exp,
  184.            "sqrt", sqrt,
  185.            0, 0
  186.          };
  187.      
  188.      /* The symbol table: a chain of `struct symrec'.  */
  189.      symrec *sym_table = (symrec *)0;
  190.      
  191.      init_table ()  /* puts arithmetic functions in table. */
  192.      {
  193.        int i;
  194.        symrec *ptr;
  195.        for (i = 0; arith_fncts[i].fname != 0; i++)
  196.          {
  197.            ptr = putsym (arith_fncts[i].fname, FNCT);
  198.            ptr->value.fnctptr = arith_fncts[i].fnct;
  199.          }
  200.      }
  201.  
  202. By simply editing the initialization list and adding the necessary
  203. include files, you can add additional functions to the calculator.
  204.  
  205. Two important functions allow look-up and installation of symbols in
  206. the symbol table.  The function `putsym' is passed a name and the
  207. type (`VAR' or `FNCT') of the object to be installed.  The object is
  208. linked to the front of the list, and a pointer to the object is
  209. returned.  The function `getsym' is passed the name of the symbol to
  210. look up.  If found, a pointer to that symbol is returned; otherwise
  211. zero is returned.
  212.  
  213.      symrec *
  214.      putsym (sym_name,sym_type)
  215.           char *sym_name;
  216.           int sym_type;
  217.      {
  218.        symrec *ptr;
  219.        ptr = (symrec *) malloc (sizeof(symrec));
  220.        ptr->name = (char *) malloc (strlen(sym_name)+1);
  221.        strcpy (ptr->name,sym_name);
  222.        ptr->type = sym_type;
  223.        ptr->value.var = 0; /* set value to 0 even if fctn.  */
  224.        ptr->next = (struct symrec *)sym_table;
  225.        sym_table = ptr;
  226.        return ptr;
  227.      }
  228.      
  229.      symrec *
  230.      getsym (sym_name)
  231.           char *sym_name;
  232.      {
  233.        symrec *ptr;
  234.        for (ptr = sym_table; ptr != (symrec *) 0;
  235.             ptr = (symrec *)ptr->next)
  236.          if (strcmp (ptr->name,sym_name) == 0)
  237.